package com.rabbit.mq.web;

import com.rabbit.mq.util.MsgDataUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @version V1.0.0
 * @ClassName: {@link AckSendController}
 * @Description: SendMessageController
 * @author: Lanzhou
 * @date: 2021/3/17 9:58
 * @Copyright:2020 All rights reserved.
 */
@RestController
@RequestMapping("/ack")
public class AckSendController {

    @Resource
    private RabbitTemplate ackRabbitTemplate;

    /**
     * ①消息推送到server，但是在server里找不到交换机 -> ConfirmCallback
     */
    @RequestMapping("/sent1")
    public String sent1() {
        Map<String, String> map = MsgDataUtil.buildMsg();
        String messageData = "message: non-existent-exchange test message !";
        map.put("messageData", messageData);
        ackRabbitTemplate.convertAndSend("non-existent-exchange", "TestDirectRouting", map);
        return "{\"code\":200,\"msg\":\"SUCCESS\"}";
    }


    /**
     * ①消息推送到server，有交换机无队列 -> RetrunCallback
     */
    @RequestMapping("/sent2")
    public String sent2() {
        Map<String, String> map = MsgDataUtil.buildMsg();
        String messageData = "message: non-existent-exchange test message !";
        map.put("messageData", messageData);
        ackRabbitTemplate.convertAndSend("lonelyDirectExchange", "TestDirectRouting", map);
        return "{\"code\":200,\"msg\":\"SUCCESS\"}";
    }


    /**
     * ③消息推送到sever，交换机和队列啥都没找到  -> ConfirmCallback
     */
    @RequestMapping("/sent3")
    public String sent3() {
        Map<String, String> map = MsgDataUtil.buildMsg();
        String messageData = "message: non-existent-exchange-route test message !";
        map.put("messageData", messageData);
        ackRabbitTemplate.convertAndSend("non-existent-exchange", "NoDirectRouting", map);
        return "{\"code\":200,\"msg\":\"SUCCESS\"}";
    }


    /**
     *  ④这种情况触发的是 ConfirmCallback 回调函数
     */
    @RequestMapping("/sent")
    public String sent() {
        Map<String, String> map = MsgDataUtil.buildMsg();
        String messageData = "this is message is ack !";
        map.put("messageData", messageData);
        ackRabbitTemplate.convertAndSend("exchangeAck", "queue_exchange_ack", map);
        return "{\"code\":200,\"msg\":\"SUCCESS\"}";
    }


}
